package com.hc.dao;

import com.hc.bean.OrderInfo;
import com.hc.bean.Pager;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrderInfoDao {
    /*模糊分页查询*/
    /*
     * 这里的小于号不能识别,要使用&lt;
     * &lt;和'='之间不能有空格
     * */
    @Select("<script>" +
            " select * from order_info " +
            " <if test='orderInfo!= null'>" +
                " <where>" +
                " <if test='orderInfo.id!= null'>" +
                "  and id like concat('%',#{orderInfo.id},'%') " +
                " </if>" +
                " <if test='orderInfo.uid!= null and orderInfo.uid>0'>" +
                " and uid like concat('%',#{orderInfo.uid},'%') " +
                " </if>" +
                " <if test='orderInfo.status!= null'>" +
                " and status like concat('%',#{orderInfo.status},'%') " +
                " </if>" +
                " <if test='orderInfo.orderTimeFrom!= null'>" +
                " and ordertime &gt;=#{orderInfo.orderTimeFrom}" +
                " </if>" +
                " <if test='orderInfo.orderTimeTo!= null'>" +
                " and ordertime &lt;=#{orderInfo.orderTimeTo}" +
                " </if>" +
                " </where>" +
            " </if>" +
            " limit #{pager.firstLimitParam},#{pager.pageRows}" +
            " </script>")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "uid",property = "userInfo",one = @One(select = "com.hc.dao.UserInfoDao.getUserInfoById"))
    })
    public List<OrderInfo> getOrderInfoList(@Param("orderInfo") OrderInfo orderInfo, @Param("pager") Pager pager);

    /*
    * 获取查询结果总记录数
    * count(*) 不能漏,
    * 否者会报错误:Expected one result (or null) to be returned by selectOne(), but found: 2] with root cause
    * 错误2:There is no getter for property named 'orderInfo' in 'class com.hc.bean.OrderInfo'
    * 原因：只有一个参数的时候,我加了<if test='orderInfo!= null'>
    *
    * */
    /*
    * 这里的小于号不能识别,要使用&lt;
    * &lt;和'='之间不能有空格
    * */
    @Select("<script>" +
            " select count(*) from order_info " +
            " <where>" +
            " <if test='id!= null'>" +
            " and id like concat('%',#{id},'%') " +
            " </if>" +
            " <if test='uid!= null and uid>0'>" +
            " and uid like concat('%',#{uid},'%') " +
            " </if>" +
            " <if test='status!= null'>" +
            " and status like concat('%',#{status},'%') " +
            " </if>" +
            " <if test='orderTimeFrom!= null'>" +
            " and ordertime &gt;=#{orderTimeFrom}" +
            " </if>" +
            " <if test='orderTimeTo!= null'>" +
            " and ordertime &lt;=#{orderTimeTo}" +
            " </if>" +
            " </where>" +
            " </script>")
    public int getOrderInfoCount(OrderInfo orderInfo);

    /*根据id查询订单信息*/
    @Select("select * from order_info where id=#{id}")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "uid",property = "userInfo",one = @One(select = "com.hc.dao.UserInfoDao.getUserInfoById"))
    })
    public OrderInfo getOrderInfoById(int id);

    /*添加新订单*/
    @Insert("insert into order_info values(#{id},#{uid},#{status},#{ordertime},#{orderprice})")
    /*设置自动获取主键,执行完成后会自动给参数orderInfo的主键属性赋值*/
    @Options(useGeneratedKeys = true,keyProperty = "id")
    public int addOrderInfo(OrderInfo orderInfo);

    /*删除订单*/
    @Delete("delete  from order_info where id in (${oids})")
    public int deleteOrderInfoByOid(String oids);
}
